package com.joshua.ticket.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map.Entry;

public class JsonOperation implements java.io.Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String className;
	private HashMap<String,String> data;
	private String operation;
	private Object entityData;


	

	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	
	public HashMap<String, String> getData() {
		return data;
	}
	public void setData(HashMap<String, String> data) {
		this.data = data;
	}
	
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	public Object getEntityData() {
		return entityData;
	}
	public void setEntityData(Object entityData) {
		this.entityData = entityData;
	}
	
	public void convertToEntityData()
	{
		ClassLoader contextClassLoader =   Thread.currentThread().getContextClassLoader();
		try {
			Class eClass = contextClassLoader.loadClass(className);
			
			if(eClass != null)
			{
				Object oData = eClass.newInstance();
				
				for(String fieldName : data.keySet())
				{
					Field field = eClass.getField(fieldName);
					if(field != null)
					{
						String fieldType = field.getType().toString();
						if(fieldType.equals("int") || fieldType.equals("Integer"))
						{
							field.set(oData, Integer.parseInt(data.get(fieldName)));
						}
						else if(fieldType.equals("Long") || fieldType.equals("long"))
						{
							field.set(oData, Long.parseLong(data.get(fieldName)));
						}
					}
				}
				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
